package com.example.controller.statistics;

import com.example.pojo.Course;
import com.example.service.statistics.CoursePrimaryService;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/*
 *  @ClassName:CoursePrimaryController
 *  @Author:WZ
 *  @DateTime:2023/1/30/19:08
 *   课程统计 -- 课程搜索关键词
 */
@CrossOrigin
@RestController
@RequestMapping("/CoursePrimary")
public class CoursePrimaryController {
    private static List<Course> daoChu;

    @Autowired
    private CoursePrimaryService coursePrimaryService;

    //查询最近7天的数据 ;
    @RequestMapping("/getCoursePrimaryZj7t")
    public List<Course> getCoursePrimaryZj7t(){
        List<Course> list7t = coursePrimaryService.getCoursePrimaryZj7t();
        return list7t;
    }

    //查询最近30天的数据 ;
    @RequestMapping("/getCoursePrimaryZj30t")
    public List<Course> getCoursePrimaryZj30t(){
        List<Course> list30t = coursePrimaryService.getCoursePrimaryZj30t();
        return list30t;
    }

    //查询最近90天的数据 ;
    @RequestMapping("/getCoursePrimaryZj90t")
    public List<Course> getCoursePrimaryZj90t(){
        List<Course> list90t = coursePrimaryService.getCoursePrimaryZj90t();
        return list90t;
    }

    //全查
    @RequestMapping("/getCoursePrimary")
    public List<Course> getCoursePrimary() {
        List<Course> list = coursePrimaryService.getCoursePrimary();
        return list;
    }

    //根据日期时间进行模糊查询
    @RequestMapping("/getCoursePrimaryLike")
    public List getCoursePrimaryLike(@RequestParam("kaiShiLike")String s1, @RequestParam("jieShuLike")String s2) {
        List<Course> coursePrimaryLike = coursePrimaryService.getCoursePrimaryLike(s1, s2);
        daoChu = coursePrimaryLike;
        return coursePrimaryLike;
    }

    //    数据导出
    @PostMapping("/export")
    public void export(HttpServletResponse response)  throws Exception{
        System.out.println(daoChu);
        // 创建工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建sheet
        HSSFSheet sheet = workbook.createSheet("sheet1");
        String fileName = "课程搜索关键词.xls"; // 设置要导出的文件的名字
        // 获取数据集合
        List<Course> list = daoChu;
        // 生成标题行
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("课程ID");
        row.createCell(1).setCellValue("课程名称");
        row.createCell(2).setCellValue("搜索关键词");
        row.createCell(3).setCellValue("课程简介");
        row.createCell(4).setCellValue("课程详情");
        row.createCell(5).setCellValue("课程时长");

        Course entity=null;

        for (int i = 0; i < list.size(); i++) {
            entity = list.get(i);

            row = sheet.createRow(i+1); // 从第2行开始填充数据
            // 序号
            row.createCell(0).setCellValue(String.valueOf(i+1));
            if (entity.getCourseName() != null){
                row.createCell(1).setCellValue(entity.getCourseName());
            }else {
                row.createCell(1).setCellValue("null");
            }

            if (entity.getCoursePrimary() != null){
                row.createCell(2).setCellValue(entity.getCoursePrimary());
            }else {
                row.createCell(2).setCellValue("null");
            }

            if (entity.getCourseIntro() != null){
                row.createCell(3).setCellValue("null");
            }else {
                row.createCell(3).setCellValue(entity.getCourseIntro());
            }

            if (entity.getCourseInfo() != null){
                row.createCell(4).setCellValue(entity.getCourseInfo());
            }else {
                row.createCell(4).setCellValue("null");
            }

            if (entity.getCourseTime() != null){
                row.createCell(5).setCellValue(entity.getCourseTime());
            }else {
                row.createCell(5).setCellValue("null");
            }
        }
        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition",
                "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        response.flushBuffer();

        workbook.write(response.getOutputStream());
    }
}
